package com.woniu.rear.comm.web.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import com.woniu.rear.comm.core.code.FileUploadErrorEnum;
import com.woniu.rear.comm.web.config.MinioConfig;
import com.woniu.rear.comm.web.exception.RearFileUploadException;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import lombok.extern.slf4j.Slf4j;

/**
 * minio操作工具类
 * @author liwei
 *
 */
@Slf4j
public class MinioTemplate {
	
	/*参数对象*/
	private MinioConfig mc;

	/*连接对象*/
    private MinioClient client;
    
    /**
              *    初始化
     * @param mc 连接参数，包括key
     */
    public MinioTemplate(MinioConfig mc) {
    	this.mc = mc;
    	this.client = MinioClient.builder()
			.endpoint(mc.getEndpoint())
			.credentials(mc.getAccessKey(), mc.getSecretKey()).build();
    }
    
    /**
              *   从流里面读取文件进行上传
     * @param fileName    文件名
     * @param bucketName  bucket名
     * @param stream      流
     * @param size        大小
     * @param contentType 文件类型
     * @return
     */
    public String uploadFile(String fileName,String bucketName,InputStream stream,Long size,String contentType) {
    	PutObjectArgs poa = PutObjectArgs.builder()
				.object(fileName).bucket(bucketName).stream(stream, size, mc.getPartSize())
				.contentType(contentType)
				.build();
    	try {
			client.putObject(poa);
			return mc.getEndpoint() + "/"+bucketName  + fileName;
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
				| IOException e) {
			log.error("上传失败: ",e);
			throw new RearFileUploadException(FileUploadErrorEnum.UPLOAD_ERROR);
		}
    }
    
    /**
              *    从流里面读取文件进行上传
     * @param fileName
     * @param bucketName
     * @param stream
     * @param size
     * @return
     */
    public String uploadFile(String fileName,String bucketName,InputStream stream,Long size) {
    	return uploadFile(fileName,bucketName,stream,size,null);
    }
	
    /**
              *    删除文件
     * @param fileName
     * @param bucketName
     * @return
     */
    public boolean deleteFile(String fileName,String bucketName) {
    	try {
    		RemoveObjectArgs roa = RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build();
			client.removeObject(roa);
			return true;
		} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
				| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
				| IOException e) {
			log.error("删除失败: ",e);
			throw new RearFileUploadException(FileUploadErrorEnum.UPLOAD_DELETE_ERROR);
		}
    }

}
